#delimit;
clear;
set more off;

global temp /Sastemp;
global path ~;
set mem 5000m;

capture log close;
log using $path/pollution/logfiles/basins.log, replace;

/*================================================
 Program: waterpoints.do
 Author : Avi Ebenstein
 Created: August 2007
=================================================*/

insheet using ~/research/pollution/GIS/water_points/WaterPoints.csv;

********************************************;
* Drop a few variables that were in chinese ;
********************************************;

drop stname2 rname4 rsname6;

****************************;
* List of chemicals         ;
****************************;

global mylist "overall_q permanganate a_n lead oils volatile_ethanol bod mercury dissolved_oxygen";

foreach i of global mylist{;
                           
gen n`i'=0;
replace n`i'=1 if `i'=="I";
replace n`i'=2 if `i'=="II";
replace n`i'=3 if `i'=="III";
replace n`i'=4 if `i'=="IV";
replace n`i'=5 if `i'=="V";
replace n`i'=6 if `i'=="VI";
replace n`i'=. if `i'=="N/A";
replace n`i'=. if n`i'==0;                           
drop `i';
rename n`i' `i';
                         };

sort objectid;

label var overall_q "Overall quality";
label var permanganate "Permanganate";
label var a_n "Ammonium nitrate";
label var lead "Lead";
label var oils "Oils";
label var volatile_ethanol "Volatile phenol";
rename volatile_ethanol volatile_phenol;
label var bod "Biological oxygen demand";
label var mercury "Mercury";
label var dissolved_oxygen "Dissolved oxygen";

label var rname4eng "River segment";
label var rsystem "River system";
label var rcode5 "River code 5";
label var stcode3 "Geographic code";

label var x "X coordinate";
label var y "Y coordinate";

save ~/research/pollution/GIS/water_points/waterpoints.dta, replace;

use ~/research/pollution/GIS/water_points/Pollutants_Counties_watersheds, clear;
keep objectid as_* level* eprov ecity ecnty gbcode gbcnty;

label var objectid "Unique identifier for water points";

label var as_bas_ "Basin";
label var as_bas_id "Basin ID";

label var level1 "Level 1 basin";
label var level2 "Level 2 basin";
label var level3 "Level 3 basin";
label var level4 "Level 4 basin";
label var level5 "Level 5 basin";
label var level6 "Level 6 basin";

label var eprov "Province";
label var ecity "City";
label var ecnty "County";
label var gbcnty "GB code (6-digit census code)";
label var gbcode "GB code (consistent with DSP code)";
sort objectid;
merge objectid using ~/research/pollution/GIS/water_points/waterpoints;
tab _merge;
keep if _merge==3;
drop _merge;

****************************;
* Re-order the water points ;
****************************;
encode rsystem,gen(rcode);
encode rsystem,gen(rcode2);
replace rcode2=11 if rcode2==1;
replace rcode2=12 if rcode2==4;
replace rcode2=13 if rcode2==6;

tabstat overall_q a_n bod oils permanganate volatile_phenol, by(rcode);
sort objectid;
save ~/research/pollution/datafiles/waterpoints_data, replace;
